package com.zhixiang.wbl.visitor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zhixiang.wbl.visitor.Helper.MyUserInfoProvider;
import com.zhixiang.wbl.visitor.R;

import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import io.rong.imlib.MessageTag;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.TypingMessage.TypingStatus;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;
import io.rong.message.TextMessage;
import io.rong.message.VoiceMessage;

public class ConversationActivity extends FragmentActivity
{
    private static final String TAG = ConversationActivity.class.getSimpleName();
    /**
     * 目标 Id
     */
    private String mTargetId;

    /**
     * 刚刚创建完讨论组后获得讨论组的id 为targetIds，需要根据 为targetIds 获取 targetId
     */
    private String mTargetIds;

    /**
     * 会话类型
     */
    private Conversation.ConversationType mConversationType;

    private String defaultTitle =null;
    @ViewInject(R.id.id_tv_title)
    TextView mTitleTV;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation);
        ViewUtils.inject(this);


        Intent intent = getIntent();
        getIntentDate(intent);
        registerInputListener();
    }
    /**
     * 展示如何从 Intent 中得到 融云会话页面传递的 Uri
     */
    private void getIntentDate(Intent intent) {

        mTargetId = intent.getData().getQueryParameter("targetId");
        mTargetIds = intent.getData().getQueryParameter("targetIds");
        //intent.getData().getLastPathSegment();//获得当前会话类型
        mConversationType = Conversation.ConversationType.valueOf(intent.getData().getLastPathSegment().toUpperCase(Locale.getDefault()));
        if (mTargetId!=null&&mConversationType== Conversation.ConversationType.PRIVATE)
        {
            UserInfo targetUserInfo = new  MyUserInfoProvider(getApplicationContext()).getUserInfo(mTargetId);
            if (targetUserInfo!=null)
            {
                defaultTitle = targetUserInfo.getName();
                mTitleTV.setText(defaultTitle);
            }
            else
            {
                Log.e(TAG, "getIntentDate: "+mTargetId );
            }
        }
        else
        {
            Log.e(TAG, "getIntentDate: "+"12" );
        }
    }

    private void registerInputListener()
    {
        //注册用户输入监听
        RongIMClient.setTypingStatusListener(new RongIMClient.TypingStatusListener() {
            @Override
            public void onTypingStatusChanged(Conversation.ConversationType type, String targetId, Collection<TypingStatus> typingStatusSet) {
                //当输入状态的会话类型和targetID与当前会话一致时，才需要显示
                if (type.equals(mConversationType) && targetId.equals(mTargetId)) {
                    //count表示当前会话中正在输入的用户数量，目前只支持单聊，所以判断大于0就可以给予显示了
                    int count = typingStatusSet.size();
                    if (count > 0) {
                        Iterator iterator = typingStatusSet.iterator();
                        TypingStatus status = (TypingStatus) iterator.next();
                        String objectName = status.getTypingContentType();

                        MessageTag textTag = TextMessage.class.getAnnotation(MessageTag.class);
                        MessageTag voiceTag = VoiceMessage.class.getAnnotation(MessageTag.class);
                        //匹配对方正在输入的是文本消息还是语音消息
                        if (objectName.equals(textTag.value())) {
                            //显示“对方正在输入”
                            mHandler.sendEmptyMessage(SET_TEXT_TYPING_TITLE);
                        } else if (objectName.equals(voiceTag.value())) {
                            //显示"对方正在讲话"
                            mHandler.sendEmptyMessage(SET_VOICE_TYPING_TITLE);
                        }
                    } else {
                        //当前会话没有用户正在输入，标题栏仍显示原来标题
                        mHandler.sendEmptyMessage(SET_TARGETID_TITLE);
                    }
                }
            }
        });
    }
    private static final int SET_TEXT_TYPING_TITLE = 1;
    private static final int SET_VOICE_TYPING_TITLE =2;
    private static final int SET_TARGETID_TITLE =3;
    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
           switch (msg.what)
           {
               case SET_TEXT_TYPING_TITLE://显示“对方正在输入”
                   mTitleTV.setText("对方正在输入");
                   break;
               case SET_VOICE_TYPING_TITLE://显示"对方正在讲话"
                   mTitleTV.setText("对方正在讲话");
                   break;
               case SET_TARGETID_TITLE://当前会话没有用户正在输入，标题栏仍显示原来标题
                   mTitleTV.setText("聊天");
                   break;
           }
        }
    };
}
